package com.qmds.userserver.user.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qmds.common.core.annotation.NoLoginCheck;
import com.qmds.common.core.entity.User;
import com.qmds.common.core.param.PhoneCode;
import com.qmds.common.core.param.UserFindPwd;
import com.qmds.common.core.utils.JwtUtils;
import com.qmds.common.core.vo.R;
import com.qmds.userserver.user.param.UpdateByIdParam;
import com.qmds.userserver.user.service.UserService;
import com.qmds.userserver.user.vo.PeopleVo;
import com.qmds.userserver.user.vo.UserVo;
import io.jsonwebtoken.Claims;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;


import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * --- 天道酬勤 ---
 *
 * @author zhanghanxuan
 * @date 2024/9/9
 * @desc
 */
@RestController
@RequestMapping("/app/user")
public class UserController {



    @Resource
    private UserService userService;

    @GetMapping("/my")
    public R<PeopleVo> my(HttpServletRequest request){
        String jwt = request.getHeader("token");// 获取请求头中的token
        Claims claims = JwtUtils.parseJWT(jwt);// 解析token
        Object uid = claims.get("uid");// 获取uid
        PeopleVo my = userService.my(Integer.valueOf(uid.toString()));// 调用service层方法
        return R.ok(my);
    }
    @GetMapping("/list")
    public R findAll(@RequestParam HashMap<String, String> map) {
        int pageSize = Integer.parseInt(map.get("pageSize"));
        int pageNumber = Integer.parseInt(map.get("pageNumber"));
        Page<PeopleVo> page = new Page<>();// 分页
        page.setSize(pageSize);
        page.setCurrent(pageNumber);
        List<PeopleVo> list = userService.findAll(page,map);
        Page<PeopleVo> iPage = new Page<>();
        iPage.setRecords(list);
        iPage.setTotal(page.getTotal());
        iPage.setSize(pageSize);
        iPage.setCurrent(pageNumber);
        return R.ok(iPage);
    }
    @GetMapping("/getType")
    public R getType() {
        return R.ok(userService.getType());
    }
    @PostMapping("/add")
    public R addUser(@RequestBody UserVo userVo) {
        if (userVo == null){
            return R.fail("请输入参数");
        }
        return R.ok(userService.addUser(userVo));
    }
    @PostMapping("/update")
    public boolean updateUser(@RequestBody UserVo userVo) {
        if (userVo == null){
            return false;
        }
        return userService.updateUser(userVo);
    }
    @GetMapping("/getEcharts")
    public R<List<Map<String,Object>>> getEcharts(Integer type) {
        return userService.getEcharts(type);
    }
    @GetMapping("/checkphone")
    public R checkPhone(String phone){
        return userService.checkPhone(phone);
    }

    @GetMapping("/sendrcode")
    @NoLoginCheck
    public R sendRcode(String phone){
        return userService.sendRcode(phone);
    }

    @PostMapping("/checkrcode")
    @NoLoginCheck
    public R checlRcode(@RequestBody PhoneCode code){
        return userService.checkRcode(code);
    }
    @PostMapping("/findpwd")
    @NoLoginCheck
    public R findPwd(@RequestBody UserFindPwd pwd)  {
        return userService.findPwd(pwd);
    }
    @PostMapping("/updateById")
    public R updateById(@RequestBody UpdateByIdParam updateByIdParam) {
        return R.ok(userService.updateById(updateByIdParam));
    }

}
